package com.redis.service.impl;

import com.redis.bean.DemoInfo;
import com.redis.repository.DemoInfoRepository;
import com.redis.service.DemoInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * DemoInfo 数据处理类
 * @author 张晓伟
 * @DATE 2018/1/11 22:18
 */
@Service
public class DemoInfoServiceImpl implements DemoInfoService {

    @Autowired
    private DemoInfoRepository demoInfoRepository;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void test(){
        ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
        valueOperations.set("mykey4", "random1="+Math.random());
        System.out.println(valueOperations.get("mykey4"));
    }

    //keyGenerator="myKeyGenerator"
    @Cacheable(value="demoInfo") //缓存,这里没有指定key.
    @Override
    public DemoInfo findById(Integer id) {
        System.err.println("DemoInfoServiceImpl.findById()=========从数据库中进行获取的....id="+id);
        DemoInfo demoInfo=demoInfoRepository.findOne(1);
        List<DemoInfo> list=demoInfoRepository.findAll();
        return demoInfoRepository.findOne(id);
    }

    @CacheEvict(value="demoInfo")
    @Override
    public void deleteFromCache(Integer id) {
        System.out.println("DemoInfoServiceImpl.delete().从缓存中删除.");
    }

}